﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Linq.Expressions;

namespace WMS.Infrastructure
{
    /// <summary>
    /// 公共仓储
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        /// <summary>
        /// 数据上下文对象
        /// </summary>
        private readonly MyDBContext _db;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<BaseRepository<T>> _logger;

        //public DbContext DbContext => _db;
        public DbContext DbContext { get;set;}

        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        /// <param name="db"></param>
        /// <param name="logger"></param>
        public BaseRepository(MyDBContext db, ILogger<BaseRepository<T>> logger)
        {
            _db = db;
            _logger = logger;
            DbContext = _db;
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IList<T>> GetAllAsync()
        {
            try
            {
                return await _db.Set<T>().ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("查询所有数据失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="expression">lambda表达式</param>
        /// <returns></returns>
        public async Task<IList<T>> GetAllAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                return await _db.Set<T>().Where(expression).ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("查询所有数据失败" + ex.Message);
                throw;
            }
        }

        public async Task<bool> InsertDataAsync(T entity)
        {
            try
            {
                await _db.Set<T>().AddAsync(entity);
                return await _db.SaveChangesAsync() > 0;
            }
            catch (Exception ex)
            {
                _logger.LogError("插入数据失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetAsync(int id)
        {
            try
            {
                var T = await _db.Set<T>().FindAsync(id);

                if (T == null)
                {
                    _logger.LogError("查询数据失败");
                    return new T();
                }

                return T;
            }
            catch (Exception ex)
            {
                _logger.LogError("查询数据失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取单个对象(通过条件)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                var T = await _db.Set<T>().Where(expression).FirstOrDefaultAsync();

                if (T == null)
                {
                    _logger.LogError("查询数据失败");
                    return new T();
                }

                return T;
            }
            catch (Exception ex)
            {
                _logger.LogError("查询数据失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateDataAsync(T entity)
        {
            try
            {
                _db.Set<T>().Update(entity);
                return await _db.SaveChangesAsync() > 0;
            }
            catch (Exception ex)
            {
                _logger.LogError("更新数据失败" + ex.Message);
                throw;
            }
        }
        //----------------------------新封装的方法----------------------------
        public async Task<int> AddNewAsync(T t)
        {
            await _db.Set<T>().AddAsync(t);
            return await _db.SaveChangesAsync();
        }

        public IQueryable<T> GetNewAllList()
        {
            return _db.Set<T>().AsQueryable();
        }



        public async Task<T> GetNewValueAsync(Expression<Func<T, bool>> expression)
        {
            var T = await _db.Set<T>().Where(expression).FirstOrDefaultAsync();

            //if (T == null)
            //{
            //    _logger.LogError("查询数据失败");
            //    return new T();
            //}

            return T;
        }

        public async Task<int> UpdateNewAsync(T t)
        {
            _db.Set<T>().Update(t);
            return await _db.SaveChangesAsync();
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetValueByIdAsync(int id)
        {
            var T = await _db.Set<T>().FindAsync(id);

            //if (T == null)
            //{
            //    _logger.LogError("查询数据失败");
            //    return new T();
            //}

            return T;
        }

        /// <summary>
        /// 根据id批量删除 真删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelDataAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                //要删除的信息
                var list = await _db.Set<T>().Where(expression).ToListAsync(); ;
                _db.Set<T>().RemoveRange(list);
                return await _db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<int> DelAll(List<T> list)
        {
            try
            {
               await  _db.AddRangeAsync(list);
                return await  _db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public async Task<int> UpdateAsync(T entity)
        {
            _db.Set<T>().Update(entity);
            return await _db.SaveChangesAsync();
        }
    }
}
